//-----------------------------------------------------------------------
// <copyright file="CustomerProductDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class CustomerProductDAOBase
    {
        #region Common methods
        public virtual CustomerProduct CreateCustomerProductFromReader(IDataReader reader)
        {
            CustomerProduct item = new CustomerProduct();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductCode"))) item.ProductCode = (string)reader["ProductCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("ReservedQty"))) item.ReservedQty = (int)reader["ReservedQty"];
                if (!reader.IsDBNull(reader.GetOrdinal("ReservedDate"))) item.ReservedDate = (DateTime)reader["ReservedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("NumberOfDay"))) item.NumberOfDay = (int)reader["NumberOfDay"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCustomerProductFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCustomerProduct methods
            
        public virtual void CreateCustomerProduct(CustomerProduct customerProduct)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsInsert");
                
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, customerProduct.UserID);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, customerProduct.ProductCode);
                dbCommandWrapper.AddInParameter("@ReservedQty", DbType.Int32, customerProduct.ReservedQty);
                dbCommandWrapper.AddInParameter("@ReservedDate", DbType.DateTime, customerProduct.ReservedDate);
                dbCommandWrapper.AddInParameter("@NumberOfDay", DbType.Int32, customerProduct.NumberOfDay);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCustomerProductException, ex);
            }
        }

        #endregion

        #region UpdateCustomerProduct methods
        
        public virtual void UpdateCustomerProduct(CustomerProduct customerProduct)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsUpdate");            
                
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, customerProduct.UserID);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, customerProduct.ProductCode);
                dbCommandWrapper.AddInParameter("@ReservedQty", DbType.Int32, customerProduct.ReservedQty);
                dbCommandWrapper.AddInParameter("@ReservedDate", DbType.DateTime, customerProduct.ReservedDate);
                dbCommandWrapper.AddInParameter("@NumberOfDay", DbType.Int32, customerProduct.NumberOfDay);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCustomerProductException, ex);
            }
        }
        
        #endregion

        #region DeleteCustomerProduct methods
        public virtual void DeleteCustomerProduct(int userID, string productCode)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsDelete");
                
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, userID);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, productCode);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCustomerProductException, ex);
            }
        }

        #endregion

        #region GetCustomerProduct methods
        
        public virtual CustomerProduct GetCustomerProduct(int userID, string productCode)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsGet");
                
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, userID);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, productCode);
                
                CustomerProduct customerProduct =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        customerProduct = CreateCustomerProductFromReader(reader);
                        reader.Close();
                    }
                }
                return customerProduct;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCustomerProductException, ex);
            }
        }
        
        #endregion

        #region GetCustomerProducts methods
        public virtual CustomerProductCollection GetCustomerProducts(CustomerProductColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                CustomerProductCollection customerProductCollection = new CustomerProductCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        CustomerProduct customerProduct = CreateCustomerProductFromReader(reader);
                        customerProductCollection.Add(customerProduct);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return customerProductCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCustomerProductsException, ex);
            }
        }
        
        public virtual CustomerProductCollection GetCustomerProducts(CustomerProductColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCustomerProducts(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}